<?php
require_once ('../../src/db/connectdatabase.php');
require_once ("../dao/DuAnDAO.php");
require_once('../../src/control/RedirectForward.php');

$url = 'admin/admin.php?c=du-an';

if(isset($_POST['act'])) {
	$action = $_POST['act'];
	$mess = "error";
	if($action == 'add') {
		if(isset($_POST['form_type']) && isset($_POST['form_title']) && isset($_POST['form_name']) && isset($_POST['form_chu_dau_tu']) && isset($_POST['form_address']) && isset($_POST['form_dien_tich']) && isset($_POST['form_tk_thi_cong']) && isset($_POST['form_content_tt']) && isset($_POST['form_content']) && isset($_POST['form_author']) && isset($_POST['form_image'])) {
			$id = time();
			$type = trim($_POST['form_type']);
			$title = trim($_POST['form_title']);
			$name = trim($_POST['form_name']);
			$chu_dau_tu = trim($_POST['form_chu_dau_tu']);
			$address = trim($_POST['form_address']);
			$dien_tich = trim($_POST['form_dien_tich']);
			$tk_thi_cong = trim($_POST['form_tk_thi_cong']);
			$content_tt = trim($_POST['form_content_tt']);
			$content = $_POST['form_content'];
			$author = trim($_POST['form_author']);
			$linkImage = trim($_POST['form_image']);
			
			$url .= '&p=add-du-an';
			if($name != "" && $type != "" && $title != "" && $chu_dau_tu != "" && $address != "" && $dien_tich != "" && $tk_thi_cong != "" && $content_tt != "" && $content != "" && $author != "" && $linkImage != "") {
				if(insertDuAn($id, $type, $title, $linkImage, $name, $chu_dau_tu, $address, $dien_tich, $tk_thi_cong, $content_tt, $content, $author)) {
					$mess = "success";
				}
			}
		}
	} else if($action == 'edit') {
		if(isset($_POST['idpro']) && $_POST['form_title'] && $_POST['form_name'] && $_POST['form_chu_dau_tu'] && $_POST['form_address'] && $_POST['form_dien_tich'] && $_POST['form_tk_thi_cong'] && $_POST['form_content_tt'] && $_POST['form_content'] && $_POST['form_author']) {
			$id = $_POST['idpro'];
			$type = trim($_POST['form_type']);
			$title = trim($_POST['form_title']);
			$name = trim($_POST['form_name']);
			$chu_dau_tu = trim($_POST['form_chu_dau_tu']);
			$address = trim($_POST['form_address']);
			$dien_tich = trim($_POST['form_dien_tich']);
			$tk_thi_cong = trim($_POST['form_tk_thi_cong']);
			$content_tt = trim($_POST['form_content_tt']);
			$content = $_POST['form_content'];
			$author = trim($_POST['form_author']);
			$linkImage = trim($_POST['form_image']);
			
			$url .= '&p=edit-du-an&id='.$id;
			if($id != "" && $name != "" && $type != "" && $title != "" && $chu_dau_tu != "" && $address != "" && $dien_tich != "" && $tk_thi_cong != "" && $content_tt != "" && $content != "" && $author != "" && $linkImage != "") {
				if(updateDuAn($id, $type, $title, $linkImage, $name, $chu_dau_tu, $address, $dien_tich, $tk_thi_cong, $content_tt, $content, $author)) {
					$mess = "success";
				}
			}
		}
	} else if($action == 'clock') {// khoa khong cho phep hien thi hang loat
		if(isset($_POST['idcheckmanager'])) {
			$url .= '&p=ds-du-an';
			$ids = $_POST['idcheckmanager'];
			if(count($ids) > 0 && displayDuAnList($ids, '0')) {
				$mess = 'success';
			}
		}
	} else if($action == 'unclock') {// cho phep hien thi hang loat
		if(isset($_POST['idcheckmanager'])) {
			$url .= '&p=ds-du-an';
			$ids = $_POST['idcheckmanager'];
			if(count($ids) > 0 && displayDuAnList($ids, '1')) {
				$mess = 'success';
			}
		}
	} else if($action == 'del') {// xoa hang loat
		if(isset($_POST['idcheckmanager'])) {
			$url .= '&p=ds-du-an';
			$ids = $_POST['idcheckmanager'];
			if(count($ids) > 0 && deleteListDuAn($ids)) {
				$mess = 'success';
			}
		}
	}
	$url .= '&mess='.$mess;
}

redirect($url);
?>